﻿Namespace Windows

    Module mWindows

        Public Enum WindowType
            AlwaysBottom = 0
            Normal = 1
            AlwaysOnTop = 2
        End Enum

        Public lstBottomWindows As New List(Of GUI.MirWindow)
        Public lstNormWindows As New List(Of GUI.MirWindow)
        Public lstOnTopWindows As New List(Of GUI.MirWindow)

        Public FocusedInterfaceObj As Windows.GUIObj = Nothing

        Public Interface GUIObj
            Sub Draw(ByRef mode As DirectX.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single)
            Function Contains(ByRef x As Integer, ByRef y As Integer) As Boolean
            Sub KeyboardInput(ByRef type As Keyboard.KeyboardInputType, ByRef e As KeyPressEventArgs)
            Sub KeyboardInput(ByRef type As Keyboard.KeyboardInputType, ByRef e As System.Windows.Forms.KeyEventArgs)
            Sub MouseInput(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs)
            Sub Focus()
            Sub UnFocus()
            Event TabSwitch()
            Sub Destroy()
            ReadOnly Property GetsFocus() As Boolean
            Sub SetPos(ByRef x As Integer, ByRef y As Integer)
            ReadOnly Property ParentWin() As GUI.MirWindow
            ReadOnly Property Name() As String
        End Interface


        Public Sub AddWindow(ByRef win As GUI.MirWindow, ByRef type As WindowType)

            Select Case type

                Case WindowType.AlwaysBottom
                    lstBottomWindows.Add(win)

                Case WindowType.Normal
                    lstNormWindows.Add(win)

                Case WindowType.AlwaysOnTop
                    lstOnTopWindows.Add(win)

            End Select

        End Sub

        Public Sub RemoveWindow(ByRef wnd As GUI.MirWindow)

            If lstBottomWindows.Contains(wnd) Then
                lstBottomWindows.Remove(wnd)
            ElseIf lstNormWindows.Contains(wnd) Then
                lstNormWindows.Remove(wnd)
            ElseIf lstOnTopWindows.Contains(wnd) Then
                lstOnTopWindows.Remove(wnd)
            End If

        End Sub

        Public Sub FocusWindow(ByRef wnd As GUI.MirWindow)
            If lstBottomWindows.Contains(wnd) Then
                If Not lstBottomWindows(lstBottomWindows.Count - 1).Equals(wnd) Then
                    lstBottomWindows.Remove(wnd)
                    lstBottomWindows.Add(wnd)
                End If
            ElseIf lstNormWindows.Contains(wnd) Then
                If Not lstNormWindows(lstNormWindows.Count - 1).Equals(wnd) Then
                    lstNormWindows.Remove(wnd)
                    lstNormWindows.Add(wnd)
                End If
            ElseIf lstOnTopWindows.Contains(wnd) Then
                If Not lstOnTopWindows(lstOnTopWindows.Count - 1).Equals(wnd) Then
                    lstOnTopWindows.Remove(wnd)
                    lstOnTopWindows.Add(wnd)
                End If
            End If
        End Sub

        Public Sub DrawWindows(ByRef mode As DirectX.DrawingMode)

            Dim i As Integer
            Dim zval As Single

            If lstBottomWindows.Count > 0 Then
                For i = 0 To lstBottomWindows.Count - 1
                    lstBottomWindows.Item(i).Draw(mode, 0.9)
                Next
            End If

            zval = 0.0

            If lstNormWindows.Count > 0 Then
                For i = 0 To lstNormWindows.Count - 1
                    lstNormWindows.Item(i).Draw(mode, 0.2 - zval)
                    zval += 0.02
                Next
            End If

            zval = 0.0

            If lstOnTopWindows.Count > 0 Then
                For i = 0 To lstOnTopWindows.Count - 1
                    lstOnTopWindows.Item(i).Draw(mode, 0.1 - zval)
                    zval += 0.02
                Next
            End If

        End Sub

        Public Sub MouseInput(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs)

                If Not Mouse.MouseObj Is Nothing Then
                    If Mouse.MouseObj.Visible = True Then
                        Dim ex As Integer = e.X - Mouse.MouseObjXOffset - Mouse.MouseObj.ParentWin.mPosX
                        Dim ey As Integer = e.Y - Mouse.MouseObjYOffset - Mouse.MouseObj.ParentWin.mPosY

                        Mouse.MouseObj.SetPos(ex, ey)

                    End If
                End If

                Dim wnd As GUI.MirWindow = GetWndAtPos(e.X, e.Y)

                If Not wnd Is Nothing Then
                    wnd.MouseInput(type, e)
            End If

        End Sub

        Public Function GetWndAtPos(ByRef x As Integer, ByRef y As Integer) As GUI.MirWindow

            Dim i As Integer

            If lstOnTopWindows.Count > 0 Then
                For i = lstOnTopWindows.Count - 1 To 0 Step -1
                    If lstOnTopWindows.Item(i).Contains(x, y) Then
                        Return lstOnTopWindows.Item(i)
                        Exit Function
                    End If
                Next
            End If


            If lstNormWindows.Count > 0 Then
                For i = lstNormWindows.Count - 1 To 0 Step -1
                    If lstNormWindows.Item(i).Contains(x, y) Then
                        Return lstNormWindows.Item(i)
                        Exit Function
                    End If
                Next
            End If

            If lstBottomWindows.Count > 0 Then
                For i = lstBottomWindows.Count - 1 To 0 Step -1
                    If lstBottomWindows.Item(i).Contains(x, y) Then
                        Return lstBottomWindows.Item(i)
                        Exit Function
                    End If
                Next
            End If

            Return Nothing

        End Function

        Public Sub KeyboardInput(ByRef type As Keyboard.KeyboardInputType, ByRef e As System.Windows.Forms.KeyEventArgs)

            If Not FocusedInterfaceObj Is Nothing Then

                Try
                    FocusedInterfaceObj.KeyboardInput(type, e)
                Catch ex As Exception
                End Try

            Else

                Editor.KeyboardInput(type, e)

            End If

        End Sub

        Public Sub KeyboardInput(ByRef type As Keyboard.KeyboardInputType, ByRef e As KeyPressEventArgs)

            If Not FocusedInterfaceObj Is Nothing Then

                Try
                    FocusedInterfaceObj.KeyboardInput(type, e)
                Catch ex As Exception
                End Try

            Else

                Editor.KeyboardInput(type, e)

            End If

        End Sub

        Public Sub FocusInterface(ByRef obj As Windows.GUIObj)

            If Not FocusedInterfaceObj Is Nothing Then FocusedInterfaceObj.UnFocus()

            If Not obj Is Nothing Then
                If obj.GetsFocus Then
                    obj.Focus()
                    FocusedInterfaceObj = obj
                Else
                    FocusedInterfaceObj = Nothing
                End If
            Else
                FocusedInterfaceObj = Nothing
            End If

        End Sub

    End Module

End Namespace
